﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.IO;

using System.Diagnostics;
using EITSurferInfinityLib.DataProcessing;

namespace EITSurferInfinityLib
{
    public class FilePlayer
    {
        Timer ReplayTimer;
        int FrameCounter;
        public PlayerStates State;

        public delegate void NewDataAvailableEventHandler(object sender, EventArgsSimpleData e);

        public event NewDataAvailableEventHandler OnNewDataAvailable;


        public FilePlayer()
        {
            ReplayTimer = new Timer();
            ReplayTimer.Interval = 100;
            FrameCounter = 0;
            ReplayTimer.Elapsed += new ElapsedEventHandler(ReplayTimer_Elapsed);
            State = PlayerStates.StaticStopped;
        }

        #region public events
        protected virtual void NewDataAvailable(EventArgsSimpleData e)
        {
            if (OnNewDataAvailable != null)
                OnNewDataAvailable(this, e);
        }

        #endregion

        #region Internal Events
        void ReplayTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            NewDataAvailable(new EventArgsSimpleData(GetSimpleData(FrameCounter)));
            FrameCounter++;
        }
        #endregion

        #region public methods
        public void SetInterval(int milliseconds)
        {
            ReplayTimer.Interval = milliseconds;
        }

        public void Open(string path)
        { }

        public void Start(int FrameOffset)
        {
            FrameCounter = FrameOffset;
            if (State == PlayerStates.StaticStopped)
            {
                State = PlayerStates.StaticStreaming;
            }
            ReplayTimer.Start();
        }
        public void Start()
        {
            Start(FrameCounter);
        }
        public void Reset()
        {
            FrameCounter = 0;
        }
        public void Stop()
        {
            ReplayTimer.Stop();
            State = PlayerStates.StaticStopped;
        }
        public int GetCurrentFrame()
        {
            return FrameCounter;
        }
        public void SetCurrentFrame(int index)
        {
            FrameCounter = index;
        }
        public SimpleData GetSimpleData(int index)
        {
            string dir = "frames";

            string path = dir + "\\" + "IQ_" + index.ToString() + ".vec";
            Int32[] v1;
            Int32[] v2;

            if (File.Exists(path))
            {


                Vector2BinFile.Read(path, out v1, out v2);
                SimpleData sd = new SimpleData(v1.Length, v2.Length);

                for (int i = 0; i < v1.Length; i++)
                {
                    sd.I[i] = v1[i];
                    sd.Q[i] = v2[i];
                }
                return sd;
            }
            else
            {
                Stop();
                return null;
            }
        }

        public double[] GetAmplitudeMeanHistory(PhysicalUnitParameter physUnit)
        {
            string dir = "frames";
            DirectoryInfo di = new DirectoryInfo(dir);
            FileInfo[] fi = di.GetFiles();

            double[] result = new double[fi.Length];
            double[] d;
            SimpleData temp;
            for (int i = 0; i < result.Length; i++)
            {
                temp=GetSimpleData(i);
                d = IQDataProcessor.GetAmplitude(temp.I, temp.Q, physUnit );
                d = IQDataProcessor.RemoveInjectionElectrode(d, 4, 32);
                result[i] = IQDataProcessor.Mean(d);
            }
            return result;
        }

        public int GetCounter()
        {
            return FrameCounter;
        }
        #endregion
    }
}
